package com.example.rizhaoscienceandtechnologymuseum.Utils;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PointF;
import android.media.FaceDetector;
import android.util.AttributeSet;
import android.util.Log;

import androidx.appcompat.widget.AppCompatImageView;

import com.example.rizhaoscienceandtechnologymuseum.View.MainActivity;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by yubo on 2015/9/5.
 * 画人脸区域的View
 */
public class FaceView extends AppCompatImageView {

    private FaceDetector.Face[] faces;
    private Paint paint;
    private Bitmap bitmap;

    private static boolean isShowKuang = true;

    private float left;
    private float top;
    private float right;
    private float bottom;

    private float pre_left;
    private float pre_top;
    private float pre_right;
    private float pre_bottom;

    private List<String> list_left = new ArrayList<>();
    private List<String> list_top = new ArrayList<>();
    private List<String> list_right = new ArrayList<>();
    private List<String> list_bottom = new ArrayList<>();

    private float old_left;
    private float old_top;

    private int x;
    private int y;
    private int width;

    public FaceView(Context context) {
        super(context);
        init(context);
    }

    public FaceView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public FaceView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private void init(Context context) {
        paint = new Paint();
        /*paint.setColor(Color.GREEN);
        paint.setStrokeWidth(5);
        paint.setStyle(Paint.Style.STROKE);//设置话出的是空心方框而不是实心方块*/
        paint.setColor(0XFF2287FE);
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeWidth(6.0f);//设置线宽
        paint.setAlpha(255);
        paint.setAntiAlias(true);

        MainActivity.setShowLvKuangListener(new MainActivity.setShowLvKuang() {
            @Override
            public void showLvKuang(boolean isShow) {
                isShowKuang = isShow;
            }
        });
    }

    public void setFaces(FaceDetector.Face[] faces) {
        if (faces != null && faces.length > 0) {
            Log.e("yubo", "FaceView setFaces, face size = " + faces.length);
            this.faces = faces;
            calculateFaceArea();
            invalidate();
        } else {
            Log.e("yubo", "FaceView setFaces, faces == null");
        }
    }

    /**
     * 计算人脸区域
     */
    private void calculateFaceArea() {
        float eyesDistance = 0;//两眼间距
        for (int i = 0; i < faces.length; i++) {
            FaceDetector.Face face = faces[i];
            if (face != null) {
                PointF pointF = new PointF();
                face.getMidPoint(pointF);//获取人脸中心点
                eyesDistance = face.eyesDistance();//获取人脸两眼的间距
                //计算人脸的区域
                float delta = eyesDistance / 2;
                left = (pointF.x - eyesDistance) / scaleRate;
                top = (pointF.y - eyesDistance + delta) / scaleRate;
                right = (pointF.x + eyesDistance) / scaleRate;
                bottom = (pointF.y + eyesDistance + delta) / scaleRate;

                x = (int) (pointF.x - eyesDistance);
                y = (int) (pointF.y - eyesDistance + delta);
                width = (int) (eyesDistance * 2);

                list_left.add(String.valueOf(left));
                list_top.add(String.valueOf(top));
                list_right.add(String.valueOf(right));
                list_bottom.add(String.valueOf(bottom));
            }
        }
        /*FaceDetector.Face face = faces[0];
        if(face != null){
            pre_bottom = bottom;
            pre_left = left;
            pre_right = right;
            pre_top = top;
            PointF pointF = new PointF();
            face.getMidPoint(pointF);//获取人脸中心点
            eyesDistance = face.eyesDistance();//获取人脸两眼的间距
            //计算人脸的区域
            float delta = eyesDistance / 2;
            left = (pointF.x - eyesDistance) / scaleRate;
            top = (pointF.y - eyesDistance + delta) / scaleRate;
            right = (pointF.x + eyesDistance) / scaleRate;
            bottom = (pointF.y + eyesDistance + delta) / scaleRate;*/

           /* x = (int) (pointF.x - eyesDistance);
            y = (int) (pointF.y - eyesDistance + delta);
            width = (int) (eyesDistance * 2);
          }*/
    }

    private float scaleRate = 1.0f;

    public void setScaleRate(float rate) {
        this.scaleRate = rate;
    }

    /**
     * 清除数据
     */
    public void clear() {
        this.faces = null;
        postInvalidate();
    }

    /**
     * 获取人脸区域，适当扩大了一点人脸区域
     */
    /*public Bitmap getFaceArea(){
        if(this.bitmap != null) {
            int bmWidth = bitmap.getWidth();
            int bmHeight = bitmap.getHeight();
            int delta = 50;
            width += 50;
            int height = width;
            x = (int) (left - delta);
            y = (int) (top - delta);
            if(x < 0) {
                x = 0;
            }
            if(y < 0) {
                y = 0;
            }
            if(width > bmWidth) {
                width = bmWidth;
            }
            if(height > bmHeight) {
                height = bmHeight;
            }
            return Bitmap.createBitmap(bitmap, x, y, width, height);
        }
        return null;
    }*/
    private void clearList() {
        list_left.clear();
        list_bottom.clear();
        list_right.clear();
        list_top.clear();
    }

    private void drawRect(Paint paint, Canvas canvas) {
        for (int i = 0; i < list_bottom.size(); i++) {
            left = Float.parseFloat(list_left.get(i));
            right = Float.parseFloat(list_right.get(i));
            top = Float.parseFloat(list_top.get(i));
            bottom = Float.parseFloat(list_bottom.get(i));
            //防抖
            if (pre_top != 0L && pre_left != 0L && Math.abs(pre_top - top) < 8 && Math.abs(pre_left - left) < 8) {
                top = pre_top;
                left = pre_left;
                right = pre_right;
                bottom = pre_bottom;
            }

            float rx = (right - left) / 5;//增加宽
            float ry = (bottom - top) / 5;//增加高
            Path path1 = new Path();
            path1.moveTo(left, top + ry);
            path1.lineTo(left, top);
            path1.lineTo(left + rx, top);
            Path path2 = new Path();
            path2.moveTo(right - rx, top);
            path2.lineTo(right, top);
            path2.lineTo(right, top + ry);
            Path path3 = new Path();
            path3.moveTo(right - rx, bottom);
            path3.lineTo(right, bottom);
            path3.lineTo(right, bottom - ry);
            Path path4 = new Path();
            path4.moveTo(left, bottom - ry);
            path4.lineTo(left, bottom);
            path4.lineTo(left + rx, bottom);
            canvas.drawPath(path1, paint);
            canvas.drawPath(path2, paint);
            canvas.drawPath(path3, paint);
            canvas.drawPath(path4, paint);
        }
        clearList();
        /*if(pre_top != 0L && pre_left != 0L && Math.abs(pre_top - top)<8 && Math.abs(pre_left - left)<8 ){
            top = pre_top;
            left = pre_left;
            right = pre_right;
            bottom = pre_bottom;
        }

        float rx = (right - left) / 5;//增加宽
        float ry = (bottom - top) / 5;//增加高
        Path path1 = new Path();
        path1.moveTo(left, top + ry);
        path1.lineTo(left, top);
        path1.lineTo(left + rx, top);
        Path path2 = new Path();
        path2.moveTo(right - rx, top);
        path2.lineTo(right, top);
        path2.lineTo(right, top + ry);
        Path path3 = new Path();
        path3.moveTo(right - rx, bottom);
        path3.lineTo(right, bottom);
        path3.lineTo(right, bottom - ry);
        Path path4 = new Path();
        path4.moveTo(left, bottom - ry);
        path4.lineTo(left, bottom);
        path4.lineTo(left + rx, bottom);
        canvas.drawPath(path1,paint);
        canvas.drawPath(path2,paint);
        canvas.drawPath(path3,paint);
        canvas.drawPath(path4,paint);*/
    }

    @Override
    protected void onDraw(final Canvas canvas) {
        super.onDraw(canvas);
        if (this.faces == null || faces.length == 0) {
            return;
        }
        canvas.drawRect(left, top, right, bottom, paint);

        if (isShowKuang) {
            drawRect(paint, canvas);
        }

//        this.clearList();
    }
}
